<!--
 * @Description: 监测月报
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:20:05
-->
<template>
  <div>
    <el-row>
      <el-col :span="5">
        <div class="empData" style="padding-right:10px;border-right:1px solid #ccc;height:calc(100vh - 60px)">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>
          <el-tree @node-click="handlerClick" class="filter-tree" :data="data" :props="defaultProps"
            :filter-node-method="filterNode" ref="tree">
          </el-tree>
        </div>
      </el-col>
      <el-col :span="19">
        <div v-if="params.device_id" class="table" style="margin-left: 10px;">
          <!-- 时间日期选择器 + 搜索按钮 -->
          <div class="header">
            <!-- {{ params.timer }} -->
            <el-date-picker value-format="timestamp" v-model="params.timer" type="datetimerange"
              :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
              align="right">
            </el-date-picker>
            <el-button type="primary" @click="toSearch" icon="el-icon-search"
              style="margin-left: 10px;background-color:#409eff;border: 0px;">搜索</el-button>
          </div>
          <!-- 表格数据 -->
          <div class="content">
            <el-table :data="empData" style="width: 100%">
              <el-table-column style="text-align: center;" prop="insert_time" label="日期" width="180">
                <template slot-scope="scope">
                  <div>
                    {{ scope.row.insert_time | fmtDate('yyyy-MM-dd hh:mm') }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="tsp" label="TSP">
              </el-table-column>
              <el-table-column prop="pm10" label="PM10">
              </el-table-column>
              <el-table-column prop="pm25" label="PM2.5">
              </el-table-column>
              <el-table-column prop="temperature" label="温度">
              </el-table-column>
              <el-table-column prop="humidity" label="湿度">
              </el-table-column>
              <el-table-column prop="wind_direction" label="风向">
              </el-table-column>
              <el-table-column prop="wind_speed" label="风速">
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页按钮 -->
          <div class="page" style="margin-top: 20%; text-align: center">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="params.page" :page-sizes="[2, 5, 10, 20]" :page-size="params.pageSize" background
              layout="total, sizes, prev, pager, next, jumper" :total="total" hide-on-single-page>
            </el-pagination>
          </div>
        </div>
        <!-- <div v-else style="color:red;text-align: center;margin: 150px;font-size: 20px;font-family: 微软雅黑;"></div> -->
        <div v-else>
          <el-empty style="margin: 150px;" description="请选择设备"></el-empty>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get } from '@/utils/request'
export default {
  data() {
    return {
      // 设备采集的数据
      empData: [],
      // 过滤文本
      filterText: '',
      // 树形结构数据源
      data: [],
      // 树形结构配置项
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      params: {
        page: 1,
        pageSize: 10,
        engineer_id: "",
        device_id: ""
      },
      // 总条目数
      total: 0,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  watch: {
    // 监听文本进行过滤 
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  computed: {},
  methods: {
    // 获取工程采集的数据 --- 右侧数据
    async getEmpData() {
      // 判断时间日期是否被选择
      if (this.params.timer) {
        this.params.begin_time = this.params.timer[0];
        this.params.end_time = this.params.timer[1];
        delete this.params.timer;
      }
      let res = await get('/monitor/pageQuery', this.params);
      // console.log(res);
      this.empData = res.data.list;
      this.total = res.data.total;
    },
    // 获取工程设备数据 --- 左侧数据
    async loadEmpData() {
      let res = await get('/engineer/findEngineerDeviceTree');
      this.data = res.data;
    },
    // 过滤节点的方法
    filterNode(value, data) {
      if (!value) return true;
      // return data.label.indexOf(value) !== -1;
      return data.name.indexOf(value) !== -1;
    },
    // 点击树型节点
    handlerClick(data) {
      // console.log(data,value);
      // data 点击工程 data --- 工程数据 有 children
      // data 点击设备 data --- 设备数据 没有children
      // 判断点击的是否是设备
      if (!data.children) {
        this.params.engineer_id = data.engineer_id;
        this.params.device_id = data.id;
      }
      this.getEmpData();
    },
    toSearch() {
      this.getEmpData();
    },
    // 页码对应的条数发生变化函数
    handleSizeChange(pageSize) {
      // console.log(pageSize,'条数');
      // 当条数发生改变时 重新请求工程数据
      this.params.pageSize = pageSize;
      // 重新获取数据
      this.getEmpData();
    },
    // 当前页发生改变的函数
    handleCurrentChange(page) {
      console.log(page, "当前页码");
      this.params.page = page;
      // 重新获取数据
      this.getEmpData();
    },
  },
  created() {
    this.loadEmpData();
    this.getEmpData();
  },
  mounted() { },
};
</script>
<style scoped></style>